package model;

/**
 * Vector.java
 * Purpose: useful to do coordinate calculations
 * 
 * @author Hans Brems
 * @version 1.0 01/12/2011
 */

/* Class to store vector information */
// TODO extend from point
public class Vector {
	private float x;
	private float y;
	
	/** Constructs a new coordinate */
	public Vector() {
	}
	
	/** Constructs a new coordinate */
	public Vector(float x, float y) {
		this.x = x;
		this.y = y;
	}
	
	/** Returns the x position of the vector */
	public float getX() {
		return this.x;
	}
	
	/** Sets the x position of the vector */
	public void setX(float x) {
		this.x = x;
	}
	
	/** Returns the y position of the vector */
	public float getY() {
		return this.y;
	}
	
	/** Sets the y position of the vector */
	public void setY(float y) {
		this.y = y;
	}
	
	/** Adds another vector to this vector */
	public void add(Vector vector) {
		this.x += vector.x;
		this.y += vector.y;
	}
	
	/** Returns horizontal distance between this and given vector  */
	public float getDistanceX(Vector destination) {
		return Math.abs(this.x - destination.x);
	}
	
	/** Returns vertical distance between this and given vector */
	public float getDistanceY(Vector destination) {
		return Math.abs(this.y - destination.y);
	}
	
	/** Returns velocity vector, assuming current vector is origin */
	public Vector getVelocity(Vector destination) {
		float velocityX = destination.x - this.x;
		float velocityY = destination.y - this.y;
		return new Vector(velocityX, velocityY);
	}
	
	/** Returns the length of the vector */
	public float getLength() {
		float xSquared = this.x * this.x;
		float ySquared = this.y * this.y;
		return (float)Math.sqrt(xSquared + ySquared);
	}
	
	/** Divides the vector into a work units */
	public void toWorkUnits() {
		float length = this.getLength();
		this.x = this.x / length;
		this.y = this.y / length;
	}
	
	/** Is vector close enough to the destination */
	public boolean atDestination(Vector destination, float justification) {
		return this.getDistanceX(destination) < justification &&
				this.getDistanceY(destination) < justification;
	}
	
	@Override
	public String toString() {
		return String.format("%f, %f", this.x, this.y);
	}
}